package com.asiainfo.bms.service.impl;

import com.asiainfo.bms.mapper.BookMapper;
import com.asiainfo.bms.model.Book;
import com.asiainfo.bms.service.BookService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 图书表 服务实现类
 * </p>
 *
 * @author liu.hx
 * @since 2021-07-13
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {
    /**
     * 分页查询
     *
     * @param page  当前页码
     * @param limit 每页记录数
     * @param book  图书
     * @return IPage类实例
     */
    @Override
    public IPage<Book> pageSelect(Long page, Long limit, Book book) {
        // 创建条件构造器
        LambdaQueryWrapper<Book> queryWrapper = Wrappers.lambdaQuery();
        if (book != null) {
            String isbn = book.getIsbn();
            String name = book.getName();
            String author = book.getAuthor();
            String press = book.getPress();
            Integer leftNum = book.getLeftNum();
            // 设置查询条件
            if (!StringUtils.isEmpty(isbn)) {
                queryWrapper.eq(Book::getIsbn, isbn);
            }
            if (!StringUtils.isEmpty(name)) {
                queryWrapper.like(Book::getName, name);
            }
            if (!StringUtils.isEmpty(author)) {
                queryWrapper.like(Book::getAuthor, author);
            }
            if (!StringUtils.isEmpty(press)) {
                queryWrapper.like(Book::getPress, press);
            }
            if (leftNum != null) {
                queryWrapper.eq(Book::getLeftNum, leftNum);
            }
        }
        return baseMapper.selectPage(new Page<>(page, limit), queryWrapper);
    }

    /**
     * 分页查询可借
     *
     * @param page  当前页码
     * @param limit 每页记录数
     * @param book  图书
     * @return IPage类实例
     */
    @Override
    public IPage<Book> pageSelectBorrow(Long page, Long limit, Book book) {
        // 创建条件构造器
        LambdaQueryWrapper<Book> queryWrapper = Wrappers.lambdaQuery();
        if (book != null) {
            String isbn = book.getIsbn();
            String name = book.getName();
            String author = book.getAuthor();
            String press = book.getPress();
            Integer leftNum = book.getLeftNum();
            // 设置查询条件
            if (!StringUtils.isEmpty(isbn)) {
                queryWrapper.eq(Book::getIsbn, isbn);
            }
            if (!StringUtils.isEmpty(name)) {
                queryWrapper.like(Book::getName, name);
            }
            if (leftNum != null) {
                queryWrapper.eq(Book::getLeftNum, leftNum);
            }
            queryWrapper.gt(Book::getLeftNum, 0);
        }
        return baseMapper.selectPage(new Page<>(page, limit), queryWrapper);
    }
}
